package com.linktrip.devicemanager.websocket.server;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.linktrip.devicemanager.util.Constant;
import com.linktrip.devicemanager.websocket.product.SocketServerThread;

public class SocketServiceLoader implements ServletContextListener{
	//socket server 线程
	private SocketServerThread socketThread;
	Logger logger = LoggerFactory.getLogger(SocketServiceLoader.class);

	public void contextDestroyed(ServletContextEvent arg0) {
		if (null != socketThread && !socketThread.isInterrupted()) {
			socketThread.stopThread();
			logger.info("================================>SocketServiceLoader stop");
			// socketThread.interrupt();
		}
	}

	public void contextInitialized(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
		if (null == socketThread) {
			logger.info("================================>SocketServiceLoader create listen at port:{}",
					Constant.DEFAULT_PORT);
			// 新建线程类
			socketThread = new SocketServerThread(Constant.DEFAULT_PORT);
			// 启动线程
			socketThread.start();
		}
	}

}

